package com.byq.mm.application.cluster.server;

import com.byq.mm.abstractx.device.Device;
import com.byq.mm.abstractx.engine.CoreEnvironment;
import com.byq.mm.abstractx.protocol.Interaction;
import com.byq.mm.application.cluster.provider.LevelControlProvider;
import com.byq.mm.application.cluster.server.executor.LevelControlExecutor;

import com.byq.mm.foundation.cluster.datatype.AttributeValue;
import com.byq.mm.foundation.cluster.datatype.DoubleRangeValue;
import com.byq.mm.foundation.cluster.datatype.DoubleValue;
import com.byq.mm.foundation.cluster.server.BasicClusterServer;

import java.util.ArrayList;
import java.util.Collection;



public class LevelControlServer extends BasicClusterServer {

	@Override
	protected void attributeWritten(String attributeName, AttributeValue value) {
		if(attributeName.equals(LevelControlProvider.Define.Attributes.VALUE)){
			DoubleValue dv = (DoubleValue)value;
			LevelControlExecutor e = (LevelControlExecutor)this.getExecutor();
			e.level(dv.getValue());
		}
	}

	public LevelControlServer(String cluster, Device device, CoreEnvironment env) {
		super(cluster, device, env);
	}

	@Override
	public boolean onCommand(Interaction i, String transport) {
		return super.onCommand(i, transport);
		
		
		
	}
	public void onValueChange(DoubleValue value){
		attributes.put(LevelControlProvider.Define.Attributes.VALUE, value);
		this.broadcastAttributeToSubscription(LevelControlProvider.Define.Attributes.VALUE);
		
	}
	public void setRange(DoubleRangeValue range){
		attributes.put(LevelControlProvider.Define.Attributes.RANGE, range);
		
		
	}
	@Override
	protected Class<? extends AttributeValue> getAttributeType(String attributeName) {
		if(attributeName.equals(LevelControlProvider.Define.Attributes.VALUE))
			return DoubleValue.class;
		if(attributeName.equals(LevelControlProvider.Define.Attributes.RANGE))
			return DoubleRangeValue.class;
		return null;
	}

	@Override
	protected void init() {
		attributes.put(LevelControlProvider.Define.Attributes.VALUE,new DoubleValue(0));
		
		attributes.put(LevelControlProvider.Define.Attributes.RANGE,  new DoubleRangeValue(0,0));
		
		subscriber.put(LevelControlProvider.Define.Attributes.VALUE,  new ArrayList<String>());
		
	}


}
